package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.adapter.PrinterSetupAdapter.ViewHolder;
import com.moleq.base.EditableListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.MultCouponSetting;
import com.moleq.models.PrinterSetting;
import com.moleq.util.MoleQUtil;

public class MultCouponAdapter extends MQBaseAdapter {
	public static final String MULTCOUPON_EDITED = "MultCoupon";
	
	private LayoutInflater mInflater;
	private List<MultCouponSetting> listMultCouponSetting;
	private EditableListener mEditableListener;
	private Context shareContext;
    private MultCouponSetting mMultCouponSetting = null;
	public MultCouponAdapter(Context context, List<MultCouponSetting> listMultCouponSetting) {
		this.mInflater = LayoutInflater.from(context);
		this.shareContext = context;
		this.listMultCouponSetting = listMultCouponSetting;
	}

	public void setEditableListener(EditableListener listener) {
		mEditableListener = listener;
	}

	public void setSource(List<MultCouponSetting> listMultCouponSetting) {
		this.listMultCouponSetting = listMultCouponSetting;
	}

	public int getCount() {
		return listMultCouponSetting.size();
	}

	public Object getItem(int position) {
		return listMultCouponSetting.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		super.getView(position, convertView, parent);
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ps_mult_coupon_row, null);
			holder.tv_mult_coupon_row_name = (TextView) convertView.findViewById(R.id.tv_mult_coupon_row_name);
			holder.tv_mult_coupon_row_multiply = (TextView) convertView.findViewById(R.id.tv_mult_coupon_row_multiply);
			holder.tv_mult_coupon_row_add_value = (TextView) convertView.findViewById(R.id.tv_mult_coupon_row_add_value);
			holder.tv_mult_coupon_row_max_value = (TextView) convertView.findViewById(R.id.tv_mult_coupon_row_max_value);
			holder.tv_mult_coupon_row_max_total = (TextView) convertView.findViewById(R.id.tv_mult_coupon_row_max_total);
			holder.iv_mult_coupon_row_active = (ImageView) convertView.findViewById(R.id.iv_mult_coupon_row_active);
			holder.ll_mult_coupon_row = (LinearLayout) convertView.findViewById(R.id.ll_mult_coupon_row);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
			mMultCouponSetting = listMultCouponSetting.get(position);
		holder.tv_mult_coupon_row_name.setText(mMultCouponSetting.getMc_cpn_name());
		holder.tv_mult_coupon_row_multiply.setText(String.valueOf(mMultCouponSetting.getMc_mult()));
		double add_value = Double.valueOf(mMultCouponSetting.getMc_add_value());
		if(add_value <= 0){
			holder.tv_mult_coupon_row_add_value.setText("");
		}else{
			holder.tv_mult_coupon_row_add_value.setText(MoleQUtil.fixNumber(add_value));
		}
		double max_value = Double.valueOf(mMultCouponSetting.getMc_max_value());
		if(max_value <= 0){
			holder.tv_mult_coupon_row_max_value.setText("");
		}else{
			holder.tv_mult_coupon_row_max_value.setText(MoleQUtil.fixNumber(max_value));
		}
		double max_total = Double.valueOf(mMultCouponSetting.getMc_max_total());
		if(max_total <= 0){
			holder.tv_mult_coupon_row_max_total.setText("");
		}else{
			holder.tv_mult_coupon_row_max_total.setText(MoleQUtil.fixNumber(max_total));
		}
		if (mMultCouponSetting.getMc_activeCheckBox()) {
			holder.iv_mult_coupon_row_active.setBackgroundResource(R.drawable.check_true);
		}else{
			holder.iv_mult_coupon_row_active.setBackgroundResource(R.drawable.check_false);
		}
		if (position == selectedRow) {
			holder.ll_mult_coupon_row.setBackgroundColor(Color.rgb(255, 219, 16));
		}else {
			holder.ll_mult_coupon_row.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}

	@Override
	public void onTextChange(int resId, String changedText) {
		// TODO Auto-generated method stub
		
	}

	public final class ViewHolder {
		public TextView tv_mult_coupon_row_name;
		public TextView tv_mult_coupon_row_multiply;
		public TextView tv_mult_coupon_row_add_value;
		public TextView tv_mult_coupon_row_max_value;
		public TextView tv_mult_coupon_row_max_total;
		public ImageView iv_mult_coupon_row_active;
		private LinearLayout ll_mult_coupon_row;
	}
}
